﻿selectMateriel = function(OutStockEditorGrid){
	var sm = new Ext.grid.CheckboxSelectionModel({
		singleSelect : false
	});
	
    var cm = new Ext.grid.ColumnModel([sm,
		{header : '物料名称ID', dataIndex : 'idt_materiel',hidden:true}, 
		{header : '物料名称', dataIndex : 'materielName'}, 
		{header : '所属部门ID', dataIndex : 'uid',hidden:true}, 
		{header : '所属部门', dataIndex : 'uname'}, 
		{header : '所持数量', dataIndex : 'amount'}, 
		{header : '属性1',    dataIndex : 'attrib1',hidden:true},
		{header : '属性2',    dataIndex : 'attrib2',hidden:true},
		{header : '属性3',    dataIndex : 'attrib3',hidden:true},
		{header : '属性4',    dataIndex : 'attrib4',hidden:true},
		{header : '属性5',    dataIndex : 'attrib5',hidden:true},
		{header : '价格',     dataIndex : 'cost',hidden:true}
    ]);
    
	cm.defaultSortable = true;

    var store = new Ext.data.Store({
       baseParams : {
			trancode : '12137'
		},
		proxy : new Ext.data.HttpProxy({
			url : '../' + WebServiceUrl
		}),
		reader : new Ext.data.JsonReader({
			totalProperty : 'totalProperty',
			root : 'root'
		},  
        [{name : 'id'},
        {name : 'uid'},
        {name : 'idt_materiel'}, 
        {name : 'amount'},
		{name : 'attrib1'}, 
		{name : 'attrib2'}, 
		{name : 'attrib3'}, 
		{name : 'attrib4'}, 
		{name : 'attrib5'}, 
		{name : 'cost'},
		{name : 'uname'},
		{name : 'materielName'}
		])
    });

    
    var grid = new Ext.grid.GridPanel({
    	id:'selectMaterielGrid',
		store: store,
		autoScroll:true,
		cm: cm,
		sm: sm,
		border:false,
		bbar: new Ext.PagingToolbar({
			pageSize : App.pageSize,
            store: store,
            displayInfo: true,
            displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
        	emptyMsg: "没有记录"
        }),
		tbar:[{
			xtype:'textfield',
			id:'keyword',
			emptyText:'请输入关键字',
			selectOnFocus : true,
			listeners :{	//注册回车查询事件
                specialKey :function(field,e){
                    if (e.getKey() == Ext.EventObject.ENTER) doSearch();
                }
			 }
		},{
			iconCls:'search',
			text:'搜索',
			handler:doSearch
		}],
		loadMask : {
			msg : '正在加载数据，请稍候。。。'
		}
    });
    store.load({params : {start : 0,limit : App.pageSize}});
	
    grid.on('rowdblclick',function(Grid,rowIndex){
    	var records = Grid.getSelectionModel().getSelected();
    	addRec(records);
    	OutStockEditorGrid.startEditing(0,3);
    	Ext.getCmp('selectMaterielFrame').close();
    })
    
	var panel;
	if(!panel){
		panel = new Ext.Panel({
		border:false,
		layout:'border',
		items:[{
			region:'center',
			layout:'fit',
		    	border:false,
		    	items:grid
		    }]
		});
	}
	
	var win;
	if(!win){
		win = new Ext.Window({
			layout: 'fit',
            id:'selectMaterielFrame',
            width: 550,
            height: 400,
            buttonAlign:'center',
            resizable: true,
            modal:true,
            plain:true,
            title: '库存信息',
            items: panel,
            buttons:[{
            	text:'选择',
            	handler:function(){
					var records = Ext.getCmp('selectMaterielGrid').getSelectionModel().getSelections();
					addRec(records);
					OutStockEditorGrid.startEditing(0,3);
    				win.close();
            	}
            },{
            	text:'取消',
            	handler:function(){
            		win.close();
            	}
            }]
        });
	}
	win.show();
	
	function doSearch(){
		var condition = Ext.get('keyword').getValue();
		store.baseParams = {
			condition : condition,
			trancode : '12137'
		};
		store.load({
			params : {
				start : 0,
				limit : App.pageSize
			}
		});
	}
	
	function addRec(records){
		var stockEditorStore = OutStockEditorGrid.getStore();
    	if(!records.length){
    		var recDefault=new Ext.data.Record({
	    		matereilName:'',		
	    		batchno:'0',
	    		kind:'0',
	    		type:'0',
	    		serialNo:'0',
	    		factcode:'0',
	    		otlcode:'0',
	    		batchid:'0',
	    		amount:'1',
	    		in_price:'0',
	    		tax_money:'0',
	    		price:'0',
	    		out_price:'0',
	    		prod_date:new Date,
	    		remark:''
			});
    		recDefault.set('materielName',records.get('materielName'));
    		OutStockEditorGrid.stopEditing();
    		recDefault.commit();
	    	stockEditorStore.insert(0,recDefault);
    	}
    	else{
    		for(var i=0;i<records.length;i++){
				var recDefault=new Ext.data.Record({//为每条新建的记录赋个初始值
		    		matereilName:'',		
		    		batchno:'0',
		    		kind:'0',
		    		type:'0',
		    		serialNo:'0',
		    		factcode:'0',
		    		otlcode:'0',
		    		batchid:'0',
		    		amount:'1',
		    		in_price:'0',
		    		tax_money:'0',
		    		price:'0',
		    		out_price:'0',
		    		prod_date:new Date,
		    		remark:''
				});
				recDefault.set('idt_Materiel',records[i].get('idt_materiel'));
    			recDefault.set('materielName',records[i].get('materielName'));
    			recDefault.set('uid',records[i].get('uid'));
				OutStockEditorGrid.stopEditing();
				recDefault.commit();
				stockEditorStore.insert(0,recDefault);
    		}
    	}
	}
}